home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / AppDomainLoader / LoadAsm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  5.7 KB  |  178 lines

  1. unit LoadAsm;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.Drawing, System.Collections, System.ComponentModel,
  7.   System.Windows.Forms, System.Data, LoadAsmImpl;
  8.  
  9. type
  10.   TWinFormAssemblyLoaderGUI = class(System.Windows.Forms.Form)
  11.   {$REGION 'Designer Managed Code'}
  12.   strict private
  13.     /// <summary>
  14.     /// Required designer variable.
  15.     /// </summary>
  16.     Components: System.ComponentModel.Container;
  17.                 btnLoad: System.Windows.Forms.Button;
  18.                 btnUnload: System.Windows.Forms.Button;
  19.                 StatusBar1: System.Windows.Forms.StatusBar;
  20.                 btnMethod2: System.Windows.Forms.Button;
  21.     /// <summary>
  22.     /// Required method for Designer support - do not modify
  23.     /// the contents of this method with the code editor.
  24.     /// </summary>
  25.     procedure InitializeComponent;
  26.                 procedure btnLoad_Click(sender: System.Object; e: System.EventArgs);
  27.                 procedure btnUnload_Click(sender: System.Object; e: System.EventArgs);
  28.                 procedure btnMethod2_Click(sender: System.Object; e: System.EventArgs);
  29.   {$ENDREGION}
  30.   strict protected
  31.     /// <summary>
  32.     /// Clean up any resources being used.
  33.     /// </summary>
  34.     AsmLoader: TAssemblyLoader;
  35.     procedure Dispose(Disposing: Boolean); override;
  36.   public
  37.     constructor Create;
  38.   end;
  39.  
  40. implementation
  41. uses
  42.   System.Reflection, System.Runtime.Serialization,
  43.   System.Diagnostics, System.IO;
  44.  
  45. {$REGION 'Windows Form Designer generated code'}
  46. /// <summary>
  47. /// Required method for Designer support - do not modify
  48. /// the contents of this method with the code editor.
  49. /// </summary>
  50. procedure TWinFormAssemblyLoaderGUI.InitializeComponent;
  51. begin
  52.         Self.btnLoad := System.Windows.Forms.Button.Create;
  53.         Self.btnUnload := System.Windows.Forms.Button.Create;
  54.         Self.StatusBar1 := System.Windows.Forms.StatusBar.Create;
  55.         Self.btnMethod2 := System.Windows.Forms.Button.Create;
  56.         Self.SuspendLayout;
  57.         // 
  58.         // btnLoad
  59.         // 
  60.         Self.btnLoad.Location := System.Drawing.Point.Create(40, 16);
  61.         Self.btnLoad.Name := 'btnLoad';
  62.         Self.btnLoad.TabIndex := 0;
  63.         Self.btnLoad.Text := 'Load';
  64.         Include(Self.btnLoad.Click, Self.btnLoad_Click);
  65.         // 
  66.         // btnUnload
  67.         // 
  68.         Self.btnUnload.Location := System.Drawing.Point.Create(152, 16);
  69.         Self.btnUnload.Name := 'btnUnload';
  70.         Self.btnUnload.TabIndex := 1;
  71.         Self.btnUnload.Text := 'Unload';
  72.         Include(Self.btnUnload.Click, Self.btnUnload_Click);
  73.         // 
  74.         // StatusBar1
  75.         // 
  76.         Self.StatusBar1.Location := System.Drawing.Point.Create(0, 87);
  77.         Self.StatusBar1.Name := 'StatusBar1';
  78.         Self.StatusBar1.Size := System.Drawing.Size.Create(292, 22);
  79.         Self.StatusBar1.TabIndex := 2;
  80.         // 
  81.         // btnMethod2
  82.         // 
  83.         Self.btnMethod2.Location := System.Drawing.Point.Create(64, 56);
  84.         Self.btnMethod2.Name := 'btnMethod2';
  85.         Self.btnMethod2.Size := System.Drawing.Size.Create(128, 23);
  86.         Self.btnMethod2.TabIndex := 4;
  87.         Self.btnMethod2.Text := 'Call Assembly Method';
  88.         Include(Self.btnMethod2.Click, Self.btnMethod2_Click);
  89.         // 
  90.         // TWinFormAssemblyLoaderGUI
  91.         // 
  92.         Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  93.         Self.ClientSize := System.Drawing.Size.Create(292, 109);
  94.         Self.Controls.Add(Self.btnMethod2);
  95.         Self.Controls.Add(Self.StatusBar1);
  96.         Self.Controls.Add(Self.btnUnload);
  97.         Self.Controls.Add(Self.btnLoad);
  98.         Self.Name := 'TWinFormAssemblyLoaderGUI';
  99.         Self.Text := 'Assembly Loader';
  100.         Self.ResumeLayout(False);
  101. end;
  102. {$ENDREGION}
  103.  
  104. procedure TWinFormAssemblyLoaderGUI.Dispose(Disposing: Boolean);
  105. begin
  106.   if Disposing then
  107.   begin
  108.     if Components <> nil then
  109.       Components.Dispose();
  110.   end;
  111.   inherited Dispose(Disposing);
  112. end;
  113.  
  114. constructor TWinFormAssemblyLoaderGUI.Create;
  115. begin
  116.   inherited Create;
  117.   //
  118.   // Required for Windows Form Designer support
  119.   //
  120.   InitializeComponent;
  121.   //
  122.   // TODO: Add any constructor code after InitializeComponent call
  123.   //
  124. end;
  125.  
  126. procedure TWinFormAssemblyLoaderGUI.btnMethod2_Click(sender: System.Object; e: System.EventArgs);
  127. begin
  128.   if Assigned(AsmLoader) then
  129.     begin
  130.       // Calls a known method, in a known class, implemented in a known namespace    
  131.       AsmLoader.Invoke('TestPkgClass.TClass1', 'SayItStatically');
  132.     end else
  133.     begin
  134.       StatusBar1.Text := 'Assembly not loaded yet.';
  135.     end;
  136. end;
  137.  
  138. procedure TWinFormAssemblyLoaderGUI.btnUnload_Click(sender: System.Object; e: System.EventArgs);
  139. begin
  140.   Cursor := Cursors.WaitCursor;
  141.   try
  142.     try
  143.       AsmLoader.Free;
  144.       AsmLoader := nil;
  145.     except
  146.       on E: Exception do
  147.         StatusBar1.Text := 'Failed due to: '+E.Message;
  148.     end;
  149.   finally
  150.     Cursor := Cursors.Default;
  151.   end;
  152. end;
  153.  
  154. procedure TWinFormAssemblyLoaderGUI.btnLoad_Click(sender: System.Object; e: System.EventArgs);
  155. var
  156.  fi: FileInfo;
  157. begin
  158.   Cursor := Cursors.WaitCursor;
  159.   if not Assigned(AsmLoader) then
  160.   try
  161.     try
  162.       AsmLoader := TAssemblyLoader.Create;
  163.       fi := FileInfo.Create(Assembly.GetExecutingAssembly.Location);
  164.       AsmLoader.LoadAssembly(fi.DirectoryName+'\DynamicAssembly\TestPkg.dll');
  165. // At this point, once the assembly is loaded, it can be updated, or deleted
  166. // Subsequent invocations of this application will use the new assembly
  167. // if it has been updated.
  168.       StatusBar1.Text := 'Assembly loaded.';
  169.     except
  170.       StatusBar1.Text := 'Unable to load assembly.';
  171.     end;
  172.   finally
  173.     Cursor := Cursors.Default;
  174.   end;
  175. end;
  176.  
  177. end.
  178.